package antlr4.myantlrtest.test.grammarsv4.protobuf3.jprotoc.utill;

import antlr4.myantlrtest.test.grammarsv4.protobuf3.jprotoc.bo.ProtoInfo;
import com.google.googlejavaformat.java.Formatter;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.HashMap;

public class Tools {

    public static String currDir = System.getProperty("user.dir");
    public static String projectDir = currDir + "/src/test/java/";

    public static String beanName = "";

    public static ProtoInfo protoInfo = new ProtoInfo();
    public static String genDir = "";
    public static StringBuilder javaSource = new StringBuilder();

    public static HashMap<String, String> pb2JavaMap = new HashMap<>();
    static {
        pb2JavaMap.put("double", "double");
        pb2JavaMap.put("float", "float");
        pb2JavaMap.put("bool", "boolean");
        pb2JavaMap.put("string", "String");
        pb2JavaMap.put("bytes", "byte[]");
        pb2JavaMap.put("int32", "int");
        pb2JavaMap.put("int64", "long");
        pb2JavaMap.put("uint32", "int");
        pb2JavaMap.put("uint64", "long");
        pb2JavaMap.put("sint32", "int");
        pb2JavaMap.put("sint64", "long");
        pb2JavaMap.put("fixed32", "int");
        pb2JavaMap.put("fixed64", "long");
        pb2JavaMap.put("sfixed32", "int");
        pb2JavaMap.put("sfixed64", "long");
    }

    public static String firstUpper(String name) {
        if (name == null || name.isEmpty()) {
            return name; // 返回原始字符串以避免NullPointerException
        }
        return name.substring(0, 1).toUpperCase() + name.substring(1);
    }

    public static void write(String filePath, String content) {
        System.out.println(filePath);
        Path path = Paths.get(filePath);
        try {
            // 如果目录不存在，则创建目录
            Files.createDirectories(path.getParent());
            // 将字符串写入文件，如果文件已存在，则覆盖
            Files.write(path, content.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String formatJavaCode(String javaCode){
        try {
            return new Formatter().formatSource(javaCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return javaCode;
    }

}
